<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="600" append-to-body destroy-on-close @closed="$emit('closed')">
		<el-container>
			<el-main class="nopadding">
				<el-scrollbar>
					<el-row class="drawer-table">
						<el-col :span="24">
							<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="80px">
								<el-form-item label="规格种类" prop="productGgId">
									<el-input v-model="form.productGgId" placeholder="请输入规格种类" clearable></el-input>
								</el-form-item>
								<el-form-item label="产品" prop="productId">
									<el-input v-model="form.productId" placeholder="请输入产品" clearable></el-input>
								</el-form-item>
								<el-form-item label="规格名称" prop="typeName">
									<el-input v-model="form.typeName" placeholder="请输入规格名称" clearable></el-input>
								</el-form-item>
								<el-form-item label="关联电商产品id" prop="shoppingProductId">
									<el-input v-model="form.shoppingProductId" placeholder="请输入关联电商产品id" clearable></el-input>
								</el-form-item>
								<el-form-item label="条形码" prop="barCode">
									<el-input v-model="form.barCode" placeholder="请输入条形码" clearable></el-input>
								</el-form-item>
								<el-form-item label="计量单位" prop="unit">
									<el-input v-model="form.unit" placeholder="请输入计量单位" clearable></el-input>
								</el-form-item>
								<el-form-item label="预计进价" prop="estimatedPrice">
									<el-input v-model="form.estimatedPrice" placeholder="请输入预计进价" clearable></el-input>
								</el-form-item>
								<el-form-item label="最高进价" prop="highestPrice">
									<el-input v-model="form.highestPrice" placeholder="请输入最高进价" clearable></el-input>
								</el-form-item>
								<el-form-item label="库存下限" prop="lowerLimit">
									<el-input v-model="form.lowerLimit" placeholder="请输入库存下限" clearable></el-input>
								</el-form-item>
								<el-form-item label="库存上限" prop="inventoryLimit">
									<el-input v-model="form.inventoryLimit" placeholder="请输入库存上限" clearable></el-input>
								</el-form-item>
								<el-form-item label="图片" prop="img">
									<el-input v-model="form.img" placeholder="请输入图片" clearable></el-input>
								</el-form-item>
								<el-form-item label="备注" prop="msg">
									<el-input v-model="form.msg" placeholder="请输入备注" clearable></el-input>
								</el-form-item>
								<el-form-item label="状态" prop="status">
									<el-input v-model="form.status" placeholder="请输入状态" clearable></el-input>
								</el-form-item>
								<el-form-item label="排序" prop="seq">
									<el-input v-model="form.seq" placeholder="请输入排序" clearable></el-input>
								</el-form-item>
								<el-form-item label="最低售价" prop="sellingPrice">
									<el-input v-model="form.sellingPrice" placeholder="请输入最低售价" clearable></el-input>
								</el-form-item>
                            </el-form>
						</el-col>
					</el-row>
				</el-scrollbar>
			</el-main>
			<el-footer>
				<el-button v-if="mode!=='show'" :loading="isSaveing" @click="submit()" type="primary" size="small">保 存</el-button>
				<el-button size="small" @click="visible=false">取 消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>
<script>
	import purchaseAPI from '@/api/purchase/productGg';
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					mode: '产品规格'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					productGgId: "",
					productId: "",
					typeName: "",
					shoppingProductId: "",
					barCode: "",
					unit: "",
					estimatedPrice: "",
					highestPrice: "",
					lowerLimit: "",
					inventoryLimit: "",
					img: "",
					msg: "",
					status: "",
					seq: "",
					sellingPrice: "",
				},
				//验证规则
				rules: {
					userName: [
						{required: true, message: '请输入名称'}
					],
					userAge: [
						{pattern: /^[0-9]*$/, message: '年龄需为数字', trigger: 'blur'}
					],
					price: [
						{pattern: /^\d+(\.\d+)?$/, message: '价格需为数字', trigger: 'blur'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if(valid){
						this.isSaveing = true;
						var res = await purchaseAPI.productGg.save.post(this.form);
						this.isSaveing = false;
						if(res.code === 2000){
							this.$emit('success', this.form, this.mode);
							this.visible = false;
							this.$message.success(res.message);
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				});
			},
			//表单注入数据
			setData(data){
				this.form.productGgId = data.productGgId;
				this.form.productId = data.productId;
				this.form.typeName = data.typeName;
				this.form.shoppingProductId = data.shoppingProductId;
				this.form.barCode = data.barCode;
				this.form.unit = data.unit;
				this.form.estimatedPrice = data.estimatedPrice;
				this.form.highestPrice = data.highestPrice;
				this.form.lowerLimit = data.lowerLimit;
				this.form.inventoryLimit = data.inventoryLimit;
				this.form.img = data.img;
				this.form.msg = data.msg;
				this.form.status = data.status;
				this.form.seq = data.seq;
				this.form.sellingPrice = data.sellingPrice;
			}
		}
	}
</script>
<style>
</style>
